<?php 
namespace app\superadmin\controller;
use think\Db;
use think\Controller;
class Knowledge extends Base{
	public function index(){
		$knows=Db::name('knowledge')->paginate(10);
		$this->assign('knows',$knows);
    //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
		return $this->fetch();
	}
	//屏蔽/解除屏蔽
	public function shield($kid){
    $state=Db::name('knowbok')->where('kid',$kid)->where("kbstate='0'")->count();
    // dump($state);
    if ($state>0) {
      # code...
      $this->error('该知识体系下有子知识体系，不能封禁！');
    }else{
  $ustatenum=Db::name("knowledge")->where("kid='$kid'")->value('kstatus');

  if($ustatenum==0){
    if (Db::name("knowledge")->where("kid='$kid'")->update(['kstatus'=>1])) {
      $this->redirect('index');
    }else{
      $this->error('解除屏蔽失败');
    }

  }
  else{
    if (Db::name("knowledge")->where("kid='$kid'")->update(['kstatus'=>0])) {
      $this->redirect('index');
    }else{
      $this->error('屏蔽失败');
    }    
  }

    }
	}

	//添加知识体系
	public function addknow(){
		$data=input("post.");
		// dump($data);
		$file = request()->file('kpic');
    // dump($file);
    $picname="";
    if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'knowledgepic');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }

      }
      // dump($picname);
      $data+=['kpic'=>$picname];
      $num=Db::name('knowledge')->where('kname',$data['kname'])->count();
    if ($num>0) {
      # code...
      $this->error('用户名重名，请重新添加！');
    }
    $res=Db::name('knowledge')->insert($data);
    if ($res) {
    	# code...
    	$this->redirect('index');
    }else{
    	$this->error("添加知识体系失败！！");
    }
	}

	//修改知识体系
	public function update($kid){
		$knows=Db::name('knowledge')->where('kid',$kid)->select();
		$this->assign('knows',$knows);
		return $this->fetch();
	}
	   //更新知识体系
  public function doupdate($kid){
    // dump($sid);
    $data=input("post.");
    $file = request()->file('kpic');
    //  dump($data);
    // dump($file);
    $picname="";
    if ($file==null) {
      # code...
       $row=Db::name('knowledge')->where('kid',$kid)->select();
      $file=$row[0]['kpic'];
      $picname=$row[0]['kpic'];
       // dump($row[0]['spic']);
    }else{
     
       // if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'knowledgepic');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }
        
        $row1=Db::name('knowledge')->where('kid',$kid)->select();
        unlink('../public/static/knowledgepic/'.$row1[0]['kpic']);

     
      
    }
     $data+=['kpic'=>$picname,
              ];
    $up=Db::name('knowledge')->where('kid',$kid)->update($data);
    if ($up) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('请至少修改一条信息');
    }
  }

  //知识体系详情
  public function details($kid){
    $knows=Db::name('knowledge')->where('kid',$kid)->select();
    $boks=Db::name('knowbok')->where('kid',$kid)->select();
    $this->assign('boks',$boks);
    $this->assign('knows',$knows);
    return $this->fetch();
  }

  //添加子知识体系
  public function addbok($kid){
    // dump($kid);
    $data=input("post.");
    // dump($data);
    $res=Db::name('knowbok')->insert($data);
    if ($res) {
      # code...
      $this->redirect('details',['kid'=>$kid]);
    }else{
      $this->error('子知识体系添加失败！');
    }
  }
  //修改子知识体系

  public function detailsupdate($kbid,$kid){
    $kbs=Db::name('knowbok')->where('kbid',$kbid)->select();
    $this->assign('kbs',$kbs);
    return $this->fetch();
  }

  //提交修改内容（子知识体系）
  public function dodetailsupdate(){
    $data=input("post.");
    // dump($data);
    $res=Db::name('knowbok')->where('kbid',$data["kbid"])->update($data);
    if ($res) {
      # code...
      $this->redirect('details',['kid'=>$data["kid"]]);
    }else{
      $this->error("请至少修改一条信息！");
    }
  }

  //子知识体系封禁/解封
  public function detailsshield($kbid,$kid){
    $state=Db::name('videos')->alias('v')->where('kbid',$kbid)->where("vstatus='0'")->count();
    // dump($state);
    if ($state>0) {
      # code...
      $this->error('该子知识体系下有视频,不可封禁！');
    }else{
      $ustatenum=Db::name("knowbok")->where("kbid='$kbid'")->value('kbstate');

  if($ustatenum==0){
    if (Db::name("knowbok")->where("kbid='$kbid'")->update(['kbstate'=>1])) {
      $this->redirect('details',['kid'=>$kid]);
    }else{
      $this->error('解除屏蔽失败');
    }

  }
  else{
    if (Db::name("knowbok")->where("kbid='$kbid'")->update(['kbstate'=>0])) {
      $this->redirect('details',['kid'=>$kid]);
    }else{
      $this->error('屏蔽失败');
    }    
  }
    }
  }
}